package com.yjf.ljr.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.dataclass.IncomeSettingDataClass;
import com.yjf.ljr.dataclass.IncomeSettingDataClass.IncomeSettingInfo;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppUtil;

/**
 * 收益配置
 * 
 * @author dongxie
 *
 */
public class IncomeSettingActivity extends IjActivity implements OnClickListener {

	private static final int TASK_TYPE_SET_NONE = 1000;
	private static final int TASK_TYPE_GET_INCOME = 1001;
	private static final int TASK_TYPE_SET_INCOME = 1002;

	@ID("etIncomeSettingLimit")
	private EditText mEtIncomeSetting;
	@ID("etIncomeSettingComment")
	private EditText mEtCommentContent;
	@ID("btnIncomeSettingSave")
	private Button mBtnIncomeSettingSave;
	@ID("btnIncomeSettingCancel")
	private Button mBtnIncomeSettingCancel;;

	@ID("tvIncomeSettingUserName")
	private TextView mTvIncomeSettingUserName;
	@ID("tvIncomeSettingAccountState")
	private TextView mTvIncomeSettingAccountState;
	@ID("tvIncomeSettingRealName")
	private TextView mTvIncomeSettingRealName;
	@ID("tvIncomeSettingIDNo")
	private TextView mTvIncomeSettingIDNo;
	@ID("tvIncomeSettingPhoneNum")
	private TextView mTvIncomeSettingPhoneNum;
	@ID("tvIncomeSettingEmail")
	private TextView mTvIncomeSettingEmail;

	private IncomeSettingInfo mIncomeSettingInfo;
	private String mUserBaseId;
	private int mTaskType = TASK_TYPE_SET_NONE;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_income_setting);

		getIntentParams();
		initControls();

		if (!TextUtils.isEmpty(mUserBaseId)) {
			showProgressDialog();
			new IncomeSettingGetTask(mUserBaseId).execute();
		}
	}

	private void getIntentParams() {
		Intent i = getIntent();
		mUserBaseId = i.getStringExtra("customerId");
	}

	/** 初始化控件 **/
	private void initControls() {
		setLeftBtnClick();
		setTitleStr("收益配置");

		mBtnIncomeSettingSave.setOnClickListener(this);
		mBtnIncomeSettingCancel.setOnClickListener(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case CommonData.REQUEST_CODE_LOGIN:
				if (mTaskType == TASK_TYPE_GET_INCOME) {
					showProgressDialog();
					new IncomeSettingGetTask(mUserBaseId).execute();
				} else if (mTaskType == TASK_TYPE_SET_INCOME) {
					setLimit();
				}
				mTaskType = TASK_TYPE_SET_NONE;
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnIncomeSettingSave:
			setLimit();
			break;
		case R.id.btnIncomeSettingCancel:
			finish();
			break;
		}
	}

	private void setLimit() {
		if (mIncomeSettingInfo == null) {
			return;
		}
		String setting = mEtIncomeSetting.getText().toString();
		String comment = "";
		try {
			comment = URLEncoder.encode(mEtCommentContent.getText().toString(), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (TextUtils.isEmpty(setting)) {
			showToast("请输入设置额度！");
			return;
		}
		mIncomeSettingInfo.profit = setting;
		mIncomeSettingInfo.note = comment;

		showProgressDialog();
		new IncomeSettingSetTask(mUserBaseId, mIncomeSettingInfo).execute();
	}

	class IncomeSettingGetTask extends AsyncTask<Void, Void, String> {

		private String customerId;
		private IncomeSettingDataClass dc = new IncomeSettingDataClass();

		public IncomeSettingGetTask(String customerId) {
			super();
			this.customerId = customerId;
		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject requestobj = new RequestObject();
			requestobj.method = "appCustomerBaseInfo";
			requestobj.map.put("customerId", customerId);
			return requestDataAndGetErrorMsg(requestobj, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			if ("-1".equals(dc.code)) {
				loginWebview();
				mTaskType = TASK_TYPE_GET_INCOME;
				return;
			}
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
			} else {
				if (dc.customerBaseInfo != null) {
					AppUtil.setViewText(mEtIncomeSetting, dc.customerBaseInfo.profit);
					if (!TextUtils.isEmpty(dc.customerBaseInfo.profit)) {
						float profit = 0;
						try {
							profit = Float.parseFloat(dc.customerBaseInfo.profit.trim());
						} catch (Exception e) {
						}
						if (profit == 0) {
							mBtnIncomeSettingSave.setText("保存");
						} else {
							mBtnIncomeSettingSave.setText("修改");
						}
					}
					AppUtil.setViewText(mEtCommentContent, dc.customerBaseInfo.note);
					AppUtil.setViewText(mTvIncomeSettingUserName, dc.customerBaseInfo.userName);
					AppUtil.setViewText(mTvIncomeSettingAccountState, dc.customerBaseInfo.statues);
					AppUtil.setViewText(mTvIncomeSettingRealName, dc.customerBaseInfo.realName);
					AppUtil.setViewText(mTvIncomeSettingIDNo, dc.customerBaseInfo.certNo);
					AppUtil.setViewText(mTvIncomeSettingPhoneNum, dc.customerBaseInfo.mobile);
					AppUtil.setViewText(mTvIncomeSettingEmail, dc.customerBaseInfo.mail);
					mIncomeSettingInfo = dc.customerBaseInfo;
				}
			}
			dismissProgressDialog();
		}
	}

	/** 修改收益配置 **/
	class IncomeSettingSetTask extends AsyncTask<Void, Void, String> {

		private String customerId;
		private IncomeSettingInfo incomeSettingInfo;
		private IncomeSettingDataClass dc = new IncomeSettingDataClass();

		public IncomeSettingSetTask(String customerId, IncomeSettingInfo info) {
			super();
			this.customerId = customerId;
			this.incomeSettingInfo = info;
		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject requestobj = new RequestObject();
			requestobj.method = "appSetQuota";
			requestobj.map.put("limit", incomeSettingInfo.profit);
			requestobj.map.put("note", incomeSettingInfo.note);
			requestobj.map.put("tblBaseId", incomeSettingInfo.tblBaseId);
			requestobj.map.put("customerId", customerId);
			return requestDataAndGetErrorMsg(requestobj, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			if ("-1".equals(dc.code)) {
				loginWebview();
				mTaskType = TASK_TYPE_SET_INCOME;
				return;
			}
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
			} else {
				showToast("保存成功");
				Intent i = new Intent();
				i.putExtra("limit", incomeSettingInfo.profit + "%");
				setResult(RESULT_OK, i);
				finish();
			}
			dismissProgressDialog();
		}
	}
}